A=lambda:[*map(int,input().split())]
for _ in[0]*A()[0]:
n,m=A();a=[A()for _ in[0]*n];*b,=map(sorted,a);i=j=0
for t in zip(a,b):i,j,*_=[i for i,x,y in zip(range(m),*t)if x^y]or(i,j)
for r in a:r[i],r[j]=r[j],r[i]
print(*((i+1,j+1),[-1])[a>b])
#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
using namespace std;
const int nmax = 2e5 + 7;
vector<vector<int>>a(nmax);
int n, m, s1, s2;
void swapp(int x,int y)
{
s1 = x, s2 = y;
for(int i=0; i<n; i++)
swap(a[i][x], a[i][y]);
}
int main()
{
int tc;
cin >> tc;
while(tc--)
{
int i,j,x,s = 0, ok = 1;
s1 = s2 = 0;
cin >> n >> m;
for(i=0; i<n; i++)
for(j=0; j<m; j++)
{
cin >> x;
a[i].push_back(x);
}
for(i=0; i<n; i++)
{
vector<int> v;
map<int,int>mp;
for(j=0; j<m; j++)
{
v.push_back(a[i][j]);
mp[a[i][j]] = j;
}
sort(v.begin(),v.end());
for(j=0; j<m; j++)
if(a[i][j] != v[j] && s == 0)
{
swapp(j, mp[v[j]]);
s++;
}
}
for(i=0; i<n; i++)
{
for(j=1; j<m; j++)
{
if(a[i][j] < a[i][j-1])
ok = 0;
}
}
if(ok == 1)
cout << s1 + 1 << " " << s2 + 1;
else
cout << -1;
cout << endl;
for(i=0;i<n;i++)
a[i].clear();
}
return 0;
}
6. Zigzag Conversion | 1612B - Special Permutation |
1481. Least Number of Unique Integers after K Removals | 1035. Uncrossed Lines |
328. Odd Even Linked List | 1219. Path with Maximum Gold |
1268. Search Suggestions System | 841. Keys and Rooms |
152. Maximum Product Subarray | 337. House Robber III |
869. Reordered Power of 2 | 1593C - Save More Mice |
1217. Minimum Cost to Move Chips to The Same Position | 347. Top K Frequent Elements |
1503. Last Moment Before All Ants Fall Out of a Plank | 430. Flatten a Multilevel Doubly Linked List |
1290. Convert Binary Number in a Linked List to Integer | 1525. Number of Good Ways to Split a String |
72. Edit Distance | 563. Binary Tree Tilt |
1306. Jump Game III | 236. Lowest Common Ancestor of a Binary Tree |
790. Domino and Tromino Tiling | 878. Nth Magical Number |
2099. Find Subsequence of Length K With the Largest Sum | 1608A - Find Array |
416. Partition Equal Subset Sum | 1446. Consecutive Characters |
1618A - Polycarp and Sums of Subsequences | 1618B - Missing Bigram |